1   
2   
3   
4   
5   
6   
7   
8   
9   
10  
11  
12  
13  
14  
15  
16  
17  package com.google.common.io;
18  
19  import static com.google.common.base.Preconditions.checkNotNull;
20  
21  import java.io.IOException;
22  import java.io.InputStream;
23  import java.util.Iterator;
24  
25  import javax.annotation.Nullable;
26  
27  
28  
29  
30  
31  
32  
33  
34  final class MultiInputStream extends InputStream {
35  
36    private Iterator<? extends ByteSource> it;
37    private InputStream in;
38  
39    
40  
41  
42  
43  
44    public MultiInputStream(
45        Iterator<? extends ByteSource> it) throws IOException {
46      this.it = checkNotNull(it);
47      advance();
48    }
49  
50    @Override public void close() throws IOException {
51      if (in != null) {
52        try {
53          in.close();
54        } finally {
55          in = null;
56        }
57      }
58    }
59  
60    
61  
62  
63    private void advance() throws IOException {
64      close();
65      if (it.hasNext()) {
66        in = it.next().openStream();
67      }
68    }
69  
70    @Override public int available() throws IOException {
71      if (in == null) {
72        return 0;
73      }
74      return in.available();
75    }
76  
77    @Override public boolean markSupported() {
78      return false;
79    }
80  
81    @Override public int read() throws IOException {
82      if (in == null) {
83        return -1;
84      }
85      int result = in.read();
86      if (result == -1) {
87        advance();
88        return read();
89      }
90      return result;
91    }
92  
93    @Override public int read(@Nullable byte[] b, int off, int len) throws IOException {
94      if (in == null) {
95        return -1;
96      }
97      int result = in.read(b, off, len);
98      if (result == -1) {
99        advance();
100       return read(b, off, len);
101     }
102     return result;
103   }
104 
105   @Override public long skip(long n) throws IOException {
106     if (in == null || n <= 0) {
107       return 0;
108     }
109     long result = in.skip(n);
110     if (result != 0) {
111       return result;
112     }
113     if (read() == -1) {
114       return 0;
115     }
116     return 1 + in.skip(n - 1);
117   }
118 }